//  第一个载入的是如下格式，之后的直接用 appServe.factory()......
appServe
  .factory('UtilsService', function($cookieStore, $ionicHistory, $window, $rootScope, $state, UserService) {
    var utils = {};
    // 判断是否已经登录
    utils.isLogin = function() {
      return $window.localStorage['user'] != undefined;
      // return $cookieStore.get('user')
    }

    utils.getUser = function() {
      return angular.fromJson($window.localStorage['user']);
      // return $cookieStore.get('user')
    }

    utils.setUser = function(user) {
      if (user != null || user != undefined) {
        $window.localStorage['user'] = angular.toJson(user);
        // $cookieStore.put('user', user)
      } else {
        $window.localStorage.removeItem('user');
        // $cookieStore.remove('user');
      }
    }

    utils.isTokenExist = function() {
      return $window.localStorage['token'] != undefined;
      // return $cookieStore.get('token')
    }

    utils.getToken = function() {
      return angular.fromJson($window.localStorage['token']);
      // return $cookieStore.get('token')
    }

    utils.setToken = function(token) {
      if (token != null) {
        $window.localStorage['token'] = angular.toJson(token);
        // $cookieStore.put('token', token)
      } else {
        $window.localStorage.removeItem('token');
        // $cookieStore.remove(token)
      }
    }

    utils.isDistExist = function() {
      return $window.localStorage['dist'];
      // return $cookieStore.get('dist')
    }

    utils.getDist = function() {
      // console.log($window.localStorage['dist']);
      return angular.fromJson($window.localStorage['dist']);
      // return $cookieStore.get('dist')
    }

    utils.setDist = function(dist) {
      if (dist != null) {
        $window.localStorage['dist'] = angular.toJson(dist);
        // $cookieStore.put('dist', dist);
      } else {
        $window.localStorage.removeItem('dist');
        // $cookieStore.remove('dist')
      }
    }


    utils.clearCookie = function() {
      $window.localStorage.removeItem('token');
      // $window.localStorage.removeItem('dist');
      $window.localStorage.removeItem('user');
      // $cookieStore.remove('dist')
      // $cookieStore.remove('token')
      // $cookieStore.remove('user')
    }

    utils.getAid = function() {
      if (utils.isDistExist()) {
        return angular.fromJson($window.localStorage['dist'])['a_repository_id']
        // return $cookieStore.get('dist')['a_repository_id'];
      }
      return undefined;
    }

    utils.isResponseOk = function(response) {
      return response.state.code == 200;
    }

    utils.goBack = function() {
      $ionicHistory.goBack();
    }


    utils.logout = function() {
      UserService.logout().$promise.then(function(response) {
        if (utils.isResponseOk(response)) {
          utils.clearCookie();
          $state.transitionTo('district.list', {}, {
            reload: true
          });
          $rootScope.$broadcast('loading:showMessage', {
            message: '注销成功，请重新选择地址'
          })
        }
      })
    }

    utils.scrollto = function(from, Y, duration, callback) {
      // scroll animation loop w/ easing
      // credit https://gist.github.com/dezinezync/5487119
      var start = Date.now(),
          from = 0;

      // decelerating to zero velocity
      function easeOutCubic(t) {
        return (--t) * t * t + 1;
      }

      // scroll loop
      function scroll() {
        var currentTime = Date.now(),
          time = Math.min(1, ((currentTime - start) / duration)),
          // where .5 would be 50% of time on a linear scale easedT gives a
          // fraction based on the easing method
          easedT = easeOutCubic(time);


        if (time < 1) {
          ionic.requestAnimationFrame(scroll);

        } else {

          callback && callback();
        }
      }

      // start scroll loop
      ionic.requestAnimationFrame(scroll);
    }

    return utils;
  });
